/*===============================
 *Title      :  EncryptFileAdapter.java
 *Created  :[ 2012-3-23 下午3:23:11 ] by fanlt
 * ==============================
 * Description:   
 * Copyright  :  Copyright (c) 2012
 * Company    :  Flyfot
 * @author  <a href="fanlt@flyfot.cn">fanlt</a>
 *
 */
package org.zxstudio.encrypt;

import java.io.File;
import java.util.ArrayList;

import org.zxstudio.encrypt.MediaCursorAdapter.onItemSelectedListener;
import org.zxstudio.encrypt.utils.Constant;
import org.zxstudio.encrypt.utils.SDUtils;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author fan
 * 
 */
public class EncryptFileAdapter extends CursorAdapter {

	private static final String TAG = "EncryptFileAdapter";
	private static final boolean DEBUG = true;

	public static final int _ID =  0;
	public static final int NAME = 1;
	public static final int STUB_NAME = 2;
	public static final int PATH = 3;
	public static final int STUB_PATH = 4;
	public static final int THUMBNAILS = 5;
	public static final int FILE_TYPE = 6;
	
	private Cursor mCursor;
	private int mViewMode = VIEW_MODE;
	public static final int VIEW_MODE = 0;
	public static final int SELECT_MODE = 1;
	
	private ArrayList<Long> mSelectFiles = new ArrayList<Long>();
	private onItemSelectedListener mSelectedListener;
	private Context mContext;
	private int mFolderType = 0 ;

	/**
	 * @param context
	 * @param c
	 */
	public EncryptFileAdapter(Context context, Cursor c) {
		super(context, c);
		mCursor = c;
		mContext = context;
	}

	public void setFolderType() {
		mFolderType = 1;
	}
	public void setMode(int mode) {
		mViewMode = mode;
		clearSelectItems();
		this.notifyDataSetChanged();
	}

	public void updateSelectItem(View view ,long id) {
		if (mSelectFiles.contains(id)) {
			mSelectFiles.remove(id);
			view.findViewById(R.id.check_icon).setBackgroundResource(R.drawable.btn_check_buttonless_off);
		} else {
			mSelectFiles.add(id);
			view.findViewById(R.id.check_icon).setBackgroundResource(R.drawable.btn_check_buttonless_on);
		}
	}
	
	public void clearSelectItems() {
		mSelectFiles.clear();
	}
	
	@Override
	public void changeCursor(Cursor cursor) {
		super.changeCursor(cursor);
		mCursor = cursor;
	}

	public void selectAllItems() {
		if (mCursor == null) return;
			if(!mCursor.moveToFirst()) {
				return;
			}
			mSelectFiles.add(mCursor.getLong(0));
			while(mCursor.moveToNext()) {
				long id = mCursor.getLong(0);
				mSelectFiles.add(id);
			}
			
		this.notifyDataSetChanged();
	}
	public ArrayList<Long> getSelectedItems() {
		return mSelectFiles;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.CursorAdapter#bindView(android.view.View,
	 * android.content.Context, android.database.Cursor)
	 */
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		long id = cursor.getLong(_ID);
		Log.v(TAG,"bindview id" + id);
		String name = cursor.getString(NAME);
		String path = cursor.getString(PATH);
		byte [] thumbnails = cursor.getBlob(THUMBNAILS);
		
		ImageView iconView = (ImageView) view
				.findViewById(R.id.gridview_fileicon);
		TextView nameView = (TextView) view
				.findViewById(R.id.gridview_filename);
		ImageView selectView = (ImageView) view.findViewById(R.id.check_icon);
		if (mViewMode == VIEW_MODE) {
			selectView.setVisibility(View.GONE);
		}
		if (thumbnails != null) {
			Bitmap bm = SDUtils.Bytes2Bimap(thumbnails);
			iconView.setImageBitmap(bm);
		} else {
			iconView.setImageResource(this.getFileIcon(name));
		}
		
		nameView.setText(name);
		if (mSelectFiles.contains(id)) {
			selectView
					.setBackgroundResource(R.drawable.btn_check_buttonless_on);
		} else {
			selectView
					.setBackgroundResource(R.drawable.btn_check_buttonless_off);
		}
	}

	public int getFileIcon(String name) {
		int icon = 0;

		String str = null;
		if (mFolderType == 1) {
			str = "icon_folder";
		} else {
			str = org.zxstudio.encrypt.utils.Constant.getFileIcon(name);
		}
		
		if (str == null) {
			icon = -1;
		} else {
			Resources res = mContext.getResources();
			icon = res
					.getIdentifier(str, "drawable", mContext.getPackageName());

			if (icon <= 0)
				icon = R.drawable.icon_file;
		}

		return icon;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.CursorAdapter#newView(android.content.Context,
	 * android.database.Cursor, android.view.ViewGroup)
	 */
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return LayoutInflater.from(context).inflate(R.layout.item_gridview,
				null);
	}
	
	
	public interface onItemSelectedListener {
		public void hasItemSelected();

		public void noItemSelected();
	}
	
	public void registerItemSelectListener(onItemSelectedListener listener) {
		mSelectedListener = listener;
	}

	public void unregisterItemSelectListener() {
		mSelectedListener = null;
	}

}
